package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class LoginServlet {

    public void service(HttpRequest request, HttpResponse response){
        System.out.println("开始处理登录");

        String name = request.getParameter("usename");
        String password = request.getParameter("passeord");
        if (name == null || password == null){
            response.setEntity(new File(
                    "./webapps/myweb/login_fail.html"));
            return;
        }
        File file = new File("./users/"+name+".obj");
        if (file.exists()){
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(file))
            ){
                User u = (User) ois.readObject();
                if (password.equals(u.getPasswrod())){
                    file = new File(
                            "./webapps/myweb/login_success.html");
                    response.setEntity(file);
                }else {
                    file = new File(
                            "./webapps/myweb/login_fail.html");
                    response.setEntity(file);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }



    }
}
